package com.oracle.malldemo.dao.impl;

import com.oracle.malldemo.dao.UsersDao;
import com.oracle.malldemo.domain.Users;
import com.oracle.malldemo.util.DruidDBUtil;
import com.oracle.malldemo.util.MyUtil;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//这个类才是真正去实现数据库表的CRUD的实现类
public class  UsersDaoImpl implements UsersDao {

    //凡是更新语句都必须使用事务(Transaction)

    @Override
    public boolean save(Users u) {

       String sql = "insert into users (username,password,email,mobile,reg_date) values (?,?,?,?,?);" ;
       List<Object> argsList = new ArrayList<Object>();
       argsList.add(u.getUsername());
       argsList.add(MyUtil.encryPassword(u.getPassword()));
       argsList.add(u.getEmail());
       argsList.add(u.getMobile());
       argsList.add(u.getRegDate());
       argsList.add(u.getUid());
       try{
           DruidDBUtil.startTransaction(); //开启事务
           int results = DruidDBUtil.executeUpdate(sql,argsList);
           DruidDBUtil.commit();
           return true;
       }catch(Exception ex){
           DruidDBUtil.rollback();
           DruidDBUtil.commit();
           ex.printStackTrace();
           return false;
       }finally{
           DruidDBUtil.closeAll(); //释放资源
       }
    }

    @Override
    public boolean update(Users u) {
        String sql = "update users set username=?,password=?,email=?,mobile=?,reg_date=? where uid =? ;" ;
        List<Object> argsList = new ArrayList<Object>();
        argsList.add(u.getUsername());
        argsList.add(u.getPassword());
        argsList.add(u.getEmail());
        argsList.add(u.getMobile());
        argsList.add(u.getRegDate());
        argsList.add(u.getUid());
        try{
            DruidDBUtil.startTransaction(); //开启事务
            int results = DruidDBUtil.executeUpdate(sql,argsList);
            DruidDBUtil.commit();
            return true;
        }catch(Exception ex){
            DruidDBUtil.rollback();
            DruidDBUtil.commit();
            ex.printStackTrace();
            return false;
        }finally{
            DruidDBUtil.closeAll(); //释放资源
        }
    }

    @Override
    public Users queryUserByUid(Integer uid) {
        String sql = "select * from users where uid =?;";
        List argsList = new ArrayList();
        argsList.add(uid);
        ResultSet rs = null;
        Users u = null;
        try{
            rs = DruidDBUtil.executeQuery(sql,argsList);
            if(rs.next()){
                u = new Users();
                u.setUid(rs.getInt("uid"));
                u.setUsername(rs.getString("username"));
                u.setPassword(rs.getString("password"));
                u.setEmail(rs.getString("email"));
                u.setMobile(rs.getString("mobile"));
                u.setRegDate(rs.getString("reg_date"));

            }
            return u;
        }catch(Exception ex){
            ex.printStackTrace();
            return u;
        }finally {
            DruidDBUtil.closeAll();
        }
    }

    @Override
    public List<Users> queryAllUsers() {

        String sql = "select * from users ;";

        ResultSet rs = null;
        Users u = null;
        List<Users> usersList = new ArrayList<Users>();
        try{
            rs = DruidDBUtil.executeQuery(sql,null);
            if(rs.next()){
                u = new Users();
                u.setUid(rs.getInt("uid"));
                u.setUsername(rs.getString("username"));
                u.setPassword(rs.getString("password"));
                u.setEmail(rs.getString("email"));
                u.setMobile(rs.getString("mobile"));
                u.setRegDate(rs.getString("reg_date"));
                usersList.add(u);
            }
            return usersList;
        }catch(Exception ex){
            ex.printStackTrace();
            return usersList;
        }finally {
            DruidDBUtil.closeAll();
        }

    }

    @Override
    public boolean delete(Integer uid) {
        String sql = "delete from users where uid =?;";
        List<Object> argsList = new ArrayList<Object>();
        argsList.add(uid);
        try{
            DruidDBUtil.startTransaction();
            DruidDBUtil.executeUpdate(sql,argsList);
            DruidDBUtil.commit();
            DruidDBUtil.endTransaction();
            return true;
        }catch(Exception ex){
            ex.printStackTrace();
            DruidDBUtil.rollback();
            DruidDBUtil.commit();
            DruidDBUtil.endTransaction();
            return false;
        }finally{
            DruidDBUtil.closeAll();
        }
    }

    @Override
    public boolean reg(Users u) {
        String sql = "insert into users (username,password,email,mobile,reg_date) values (?,?,?,?,?);" ;
        List<Object> argsList = new ArrayList<Object>();
        argsList.add(u.getUsername());
        argsList.add(MyUtil.encryPassword(u.getPassword()));
        argsList.add(u.getEmail());
        argsList.add(u.getMobile());
        argsList.add(MyUtil.dateToChineseDateFormat(new Date()));
        try{
            DruidDBUtil.startTransaction(); //开启事务
            Long id = DruidDBUtil.executeUpdateWithReturnId(sql,argsList);
            //拼一个用户名
            sql = "update users set username = ? where uid=?;";
            argsList.clear();
            argsList.add("users"+id);
            argsList.add(id);
            DruidDBUtil.executeUpdate(sql,argsList);

            DruidDBUtil.commit();
            return true;
        }catch(Exception ex){
            DruidDBUtil.rollback();
            DruidDBUtil.commit();
            ex.printStackTrace();
            return false;
        }finally{
            DruidDBUtil.closeAll(); //释放资源
        }
    }

    @Override
    public Users login(String username, String password) {
        String sql = "";
        if(MyUtil.isPhoneLegal(username)) {
            sql = "select * from users where mobile=? and password=?;";
        }else if(MyUtil.isEmail(username)){
            sql = "select * from users where email=? and password=?;";
        }else{
            sql = "select * from users where username=? and password=?;";
        }
        List<Object> argsList = new ArrayList<Object>();
        argsList.add(username);
        argsList.add(MyUtil.encryPassword(password));
        ResultSet rs = null;
        Users loginSuccessUser = null;
        try{

             rs =DruidDBUtil.executeQuery(sql,argsList);
             if(rs.next()){
                 loginSuccessUser = new Users();
                 loginSuccessUser.setUid(rs.getInt("uid"));
                 loginSuccessUser.setUsername(rs.getString("username"));
                 loginSuccessUser.setPassword(rs.getString("password"));
                 loginSuccessUser.setEmail(rs.getString("email"));
                 loginSuccessUser.setMobile(rs.getString("mobile"));
                 loginSuccessUser.setRegDate(rs.getString("reg_date"));
             }
             return loginSuccessUser;
        }catch(Exception ex){
            ex.printStackTrace();
            return loginSuccessUser;
        }finally{
            DruidDBUtil.closeAll();
        }
    }

    @Override
    public Users identity(Integer uid, String password) {
        String sql = "";
        sql = "select * from users where uid=? and password=?;";
        List<Object> argsList = new ArrayList<Object>();
        argsList.add(uid);
        argsList.add(MyUtil.encryPassword(password));
        ResultSet rs = null;
        Users identitySuccessUser = null;
        try{
            rs =DruidDBUtil.executeQuery(sql,argsList);
            if(rs.next()){
                identitySuccessUser = new Users();
                identitySuccessUser.setUid(rs.getInt("uid"));
                identitySuccessUser.setUsername(rs.getString("username"));
                identitySuccessUser.setPassword(rs.getString("password"));
                identitySuccessUser.setEmail(rs.getString("email"));
                identitySuccessUser.setMobile(rs.getString("mobile"));
                identitySuccessUser.setRegDate(rs.getString("reg_date"));
            }
            return identitySuccessUser;
        }catch(Exception ex){
            ex.printStackTrace();
            return identitySuccessUser;
        }finally{
            DruidDBUtil.closeAll();
        }
    }
}
